package com.heima.article.controller;

import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.common.dtos.ArticleHomeDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/v1/article")
@Slf4j
public class ApArticleController {

    @Autowired
    private ApArticleService apArticleService;


    @PostMapping("/load")
    public ResponseResult load(@RequestBody ArticleHomeDto homeDto) {
        return apArticleService.loadHotArticle(homeDto, ArticleConstants.LOADTYPE_LOAD_INDEX);
    }

    /**
     * 加载更多
     *
     * @return
     */
    @PostMapping("loadmore")
    public ResponseResult loadmore(@RequestBody ArticleHomeDto homeDto) {
        return apArticleService.load(homeDto, ArticleConstants.LOADTYPE_LOAD_MORE);
    }

    /**
     * 加载更新
     *
     * @return
     */
    @PostMapping("loadnew")
    public ResponseResult loadnew(@RequestBody ArticleHomeDto homeDto) {
        return apArticleService.load(homeDto, ArticleConstants.LOADTYPE_LOAD_NEW);
    }


    /**
     * 打开或关闭评论
     *
     * @param articleId
     * @param isComment
     * @return {@link ResponseResult }
     */
    @GetMapping("/is_comment/{articleId}/{isComment}")
    public ResponseResult updateIsCommentStatus(@PathVariable Long articleId, @PathVariable Integer isComment) {
        log.info("打开或关闭评论远程调用的参数:{},{}", articleId, isComment);
        apArticleService.updateIsCommentStatus(articleId, isComment);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 自媒体端-评论管理的清空评论数量
     *
     * @param articleId
     */
    @GetMapping("/comment/{articleId}")
    public ResponseResult deleteAllComment(@PathVariable Long articleId) {
        log.info("要清空评论数量的ID:{}", articleId);
        apArticleService.deleteAllComment(articleId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
